$(document).ready(function () {
    form_post();
    form_ajax_get();
    form_ajax_post();
    //全选的实现
    $(".check-all").click(function () {
        $(".ids").prop("checked", this.checked);
    });
    $(".ids").click(function () {
        var option = $(".ids");
        option.each(function (i) {
            if (!this.checked) {
                $(".check-all").prop("checked", false);
                return false;
            } else {
                $(".check-all").prop("checked", true);
            }
        });
    });
});
document.onreadystatechange = subSomething;
function subSomething() {
    if (document.readyState === 'complete') {

    }
}
function form_ajax_post() {
    $('.ajax-post').click(function () {
        var target, query, form;
        var target_form = $(this).attr('target-form');
        var that = this;
        var nead_confirm = false;
        if (($(this).attr('type') == 'submit') || (target = $(this).attr('href')) || (target = $(this).attr('url'))) {
            form = $('.' + target_form);

            if ($(this).attr('hide-data') === 'true') {//无数据时也可以使用的功能
                form = $('.hide-data');
                query = form.serialize();
            } else if (form.get(0) == undefined) {
                return false;
            } else if (form.get(0).nodeName == 'FORM') {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                if ($(this).attr('url') !== undefined) {
                    target = $(this).attr('url');
                } else {
                    target = form.get(0).action;
                }
                query = form.serialize();
            } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                form.each(function (k, v) {
                    if (v.type == 'checkbox' && v.checked == true) {
                        nead_confirm = true;
                    }
                })
                if (nead_confirm && $(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.serialize();
            } else {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.find('input,select,textarea').serialize();
            }
            $(that).addClass('disabled').attr('autocomplete', 'off').prop('disabled', true);
            $.post(target, query).success(function (data) {
                if (data.status === 1) {
                    $.scojs_message(data.info, $.scojs_message.TYPE_OK);
                    if (data.url !== '') {
                        url = data.url;
                        setTimeout("self.location=url", data.time * 1000);
                    } else {
                        location.reload();
                    }
                }
                else
                if (data.status === 0) {
                    $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
                }
            }, "json").error(function (data) {
                $.scojs_message(server_error, $.scojs_message.TYPE_ERROR);
            });
        }
        return false;
    });
}
function form_ajax_get() {
    $('.ajax-get').click(function (e) {
        e.preventDefault();
        if ($(this).hasClass('confirm')) {
            if (!confirm('确认要执行该操作吗?')) {
                return false;
            }
        }
        if ((target = $(this).attr('href')) || (target = $(this).attr('url'))) {
            $.get(target, function (data) {
                if (data.status === 1) {
                    $.scojs_message(data.info, $.scojs_message.TYPE_OK);
                    if (data.url !== '') {
                        url = data.url;
                        setTimeout("self.location=url", data.time * 1000);
                    } else {
                        location.reload();
                    }
                }
                else
                if (data.status === 0) {
                    $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
                }
            }, "json").error(function (data) {
                $.scojs_message(server_error, $.scojs_message.TYPE_ERROR);
            });
        }
    });
}
function form_post() {
    $('form').submit(function (e) {
        e.preventDefault();
        if ($(this).attr('method') === 'post' && $(this).attr('ajax') !== 'no') {
            submit = $(":submit", this);
            var l = $(submit).ladda();
            l.ladda('start');
            $.post($(this).attr('action'), $(this).serialize(), function (data) {
                $(":password").val('');
                $.ladda('stopAll');
                if (data.status === 1) {
                    $.scojs_message(data.info, $.scojs_message.TYPE_OK);
                    if (data.url !== '') {
                        url = data.url;
                        setTimeout("self.location=url", data.time * 1000);
                    } else {
                        $('#ajaxModal').modal('hide');
                    }
                }
                else
                if (data.status === 0) {
                    $('#validation_code').fadeIn();
                    $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
                }
            }, "json").error(function (data) {
                $.ladda('stopAll');
                $.scojs_message(server_error, $.scojs_message.TYPE_ERROR);
            });
        }
    });
}
var email_submit, mobile_submit, code_email_times = 60, code_mobile_times = 60, submit_mobile_title, submit_email_title;
function send_email_code(url, t, text) {
    $.post(url, {email: text}, function (data) {
        if (data.status === 1) {
            email_submit = $(t);
            submit_email_title = email_submit.html();
            countdown_lock_button_email();
            $.scojs_message(data.info, $.scojs_message.TYPE_OK);
        }
        else
        if (data.status === 0) {
            $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
        }
    }, 'json').error(function (data) {
        $.scojs_message(server_error, $.scojs_message.TYPE_ERROR);
    });
}
function send_mobile_code(url, t, text) {
    $.post(url, {mobile: text}, function (data) {
        if (data.status === 1) {
            mobile_submit = $(t);
            submit_mobile_title = mobile_submit.html();
            countdown_lock_button_mobile()
            $.scojs_message(data.info, $.scojs_message.TYPE_OK);
        }
        else
        if (data.status === 0) {
            $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
        }
    }, 'json').error(function (data) {
        $.scojs_message(server_error, $.scojs_message.TYPE_ERROR);
    });
}
function countdown_lock_button_mobile() {
    mobile_submit.attr("disabled", "true");
    code_mobile_times = $.cookie('code_mobile_times') ? $.cookie('code_mobile_times') : 60;

    code_mobile_times--;

    if (code_mobile_times === 0) {
        mobile_submit.removeAttr('disabled');
        mobile_submit.html(submit_mobile_title);
        code_mobile_times = 60;
        $.removeCookie('code_mobile_times');
    } else {
        $.cookie('code_mobile_times', code_mobile_times);
        mobile_submit.html(submit_mobile_title + ' [' + code_mobile_times + ']');
        setTimeout(countdown_lock_button_mobile, 1000);
    }
}
function countdown_lock_button_email() {
    email_submit.attr("disabled", "true");
    code_email_times = $.cookie('code_email_times') ? $.cookie('code_email_times') : 60;

    code_email_times--;

    if (code_email_times === 0) {
        email_submit.removeAttr('disabled');
        email_submit.html(submit_email_title);
        code_email_times = 60;
        $.removeCookie('code_email_times');
    } else {
        $.cookie('code_email_times', code_email_times);
        email_submit.html(submit_email_title + ' [' + code_email_times + ']');
        setTimeout(countdown_lock_button_email, 1000);
    }
}

